clear
clear matrix
capture log close
set rmsg on
set more off
set more 1
set matsize 800
set scrollbufsize 500000
set mem 7g

********************************************************************************
*****                  Setting paths to file directories                    ****
********************************************************************************

* Desktop path - designate as needed *

global code "XXXXX\code_for_publication"

********************************************************************************
* Cole, Damm, Hackney, and Suleymanov (2024)                                           *
* "It's not (only) personal, it's business: Personal bankruptcy exemptions and business credit " *      *
* Review of Finance *
********************************************************************************

clear all
clear matrix

***************************************************************
************************   Figure 4   *************************
***************************************************************

* open file
	
	use "$code/total_tract.dta", clear
	global controls2 wlnpop wunempr wlfpr wmedage wlnmfi wminpop hpi_growth

		* Identify first exemption change
		gen change=homestead_change>0 & year>2005
		gen yearchange=year if change==1

		bysort fips_st: egen minyearchange=min(yearchange)
		gen treat_post=year>=minyearchange

		gen first_change=homestead_change if year==minyearchange
		bysort fips_st: egen firstchange=max(first_change)
		gen treat_post_change=treat_post*firstchange
		gen log_first_change=log(1+treat_post_change)

		tsset geoid_tract year

		gen event_year=year-minyearchange
		replace first_change=0 if first_change==.
		gen logfirst=log(1+first_change)

* Identify states with more than one change
		gen second_change=(homestead_change>0 & homestead_change!=first_change)
		gen second_change_year=year if second_change==1
		bysort fips_st: egen secondchangeyear=max(second_change_year)
		gen second_treat_post=year>=secondchangeyear 

		gen tm4=event==-4
		gen tm3=event==-3
		gen tm2=event==-2
		gen tm1=0
		gen t0=event==0
		gen tp1=event==1
		gen tp2=event==2
		gen tp3=event==3
		gen tp4=event==4
	
	reghdfe logamt4 tm4 tm3 tm2 tm1 t0 tp1 tp2 tp3 tp4 wlnpop wunempr wlfpr wmedage wlnmfi wminpop hpi_growth if year>2005, absorb(msa_year2 geoid_tract) cluster(fips_st)
	coefplot, vertical drop(_cons  wlnpop wunempr wlfpr wmedage wlnmfi wminpop hpi_growth) yline(0) omitted 

	reghdfe logamt5 tm4 tm3 tm2 tm1 t0 tp1 tp2 tp3 tp4 wlnpop wunempr wlfpr wmedage wlnmfi wminpop hpi_growth if year>2005, absorb(msa_year2 geoid_tract) cluster(fips_st)
	coefplot, vertical drop(_cons  wlnpop wunempr wlfpr wmedage wlnmfi wminpop hpi_growth) yline(0) omitted 
